﻿/*!
* jQuery.HrefQueryString  v1.0.3
*
* Copyright 2012, Maurice de Beijer
* Microsoft Reciprocal License (Ms-RL)
* See: http://hrefquerystring.codeplex.com/license
*
*/

/// <reference path="Scripts/jquery-1.4.1.js" />

(function ($) {
    function getQuery(s) {
        var query = {};

        s.replace(/\b([^&=]*)=([^&=]*)\b/g, function (m, a, d) {
            if (typeof query[a] != 'undefined') {
                query[a] += ',' + d;
            } else {
                query[a] = d;
            }
        });

        return query;
    }

    function updateQueryString(el, options) {
        var query = {};
        var href = el.attr("href");
        var i = href.indexOf("?");
        if (i >= 0) {
            query = getQuery(href.substr(i + 1));
            href = href.substr(0, i);
        }

        var settings = $.extend(query, options);

        var queryString = $.param(settings);

        var text = el.text();
        el.attr("href", href + "?" + queryString); //.text(text);
    }

    $.fn.hrefQueryString = function (options) {
        if (options) {
            if (typeof options === "string") {
                options = getQuery(options);
            }

            this.each(function () {
                var el = $(this);
                if (el.is("a[href]")) {
                    updateQueryString(el, options);
                }
            });
        }

        return this;
    };
})(jQuery);
